function CommandsPlugin(commands) {
  function onConstruct(editor, next) {
    for (const command in commands) {
      editor.registerCommand(command);
    }
    next();
  }

  function onCommand(command, editor, next) {
    const { type, args } = command;
    const fn = commands[type];
    if (!fn) {
      return next();
    }
    editor.command(fn, ...args);
  }

  return { onConstruct, onCommand };
}

export default CommandsPlugin;
